package cn.shyouchi.wuguanjia.android.ui.event.post;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import cn.shyouchi.wuguanjia.android.R;
import cn.shyouchi.wuguanjia.android.base.BaseActivity;
import cn.shyouchi.wuguanjia.android.event.DataChangeEvent;
import cn.shyouchi.wuguanjia.android.utils.LogUtil;
import cn.shyouchi.wuguanjia.android.widget.TopBar;

public class EventPostActivity extends BaseActivity implements EventPostContract.View {

    public final static int REQUEST_TYPE_ADD = 10001;
    public final static int REQUEST_TYPE_ADD_SUCCESS = 10002;

    @BindView(R.id.topBar)
    TopBar mTopBar;
    @BindView(R.id.rv_post)
    RecyclerView mRvPost;
    EventPostPresenter mEventPostPresenter;

    String projectId;

    EventPostAdapter mAdapter;

    boolean needRefresh = true;

    List<PostEntity.ResultMessageBean> mPostEntities = new ArrayList<>();

    @Override
    public int initContentView() {
        return R.layout.activity_event_post;
    }

    @Override
    public void initInjector() {
        mEventPostPresenter = new EventPostPresenter(mContext, this);

        projectId = getIntent().getStringExtra("projectId");
    }

    @Override
    public void initUiAndListener() {
        ButterKnife.bind(this);
        mTopBar.setLeftIcon(R.mipmap.ic_back);
        mTopBar.setTitle(getTitle().toString());
        setSupportActionBar(mTopBar.getToolbar());

        EventBus.getDefault().register(this);
        mAdapter = new EventPostAdapter(mPostEntities, this);
        mRvPost.setLayoutManager(new LinearLayoutManager(mContext));
        mRvPost.setAdapter(mAdapter);











    }

    @Override
    protected boolean isApplyStatusBarTranslucency() {
        return false;
    }

    @Override
    protected boolean isApplyStatusBarColor() {
        return true;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

    }

    @OnClick(R.id.btn_add_post)
    public void onClick() {
        Intent intent = new Intent(mContext, AddPostActivity.class);
        intent.putExtra("projectId", projectId);
        startActivityForResult(intent, REQUEST_TYPE_ADD);
    }


    @Override
    public void onDataRefresh(List<PostEntity.ResultMessageBean> resultMessages) {
        mPostEntities.clear();
        mPostEntities.addAll(resultMessages);
        mAdapter.notifyDataSetChanged();
    }

    @Override
    public void onItemClick(PostEntity.ResultMessageBean entity) {
        LogUtil.json(entity);
        Intent intent = new Intent(mContext, PreviewPostActivity.class);
        intent.putExtra("post", entity);
        intent.putExtra("projectId", projectId);

        startActivity(intent);
    }

    @Override
    public void onDelClick(PostEntity.ResultMessageBean entity) {
        mEventPostPresenter.delPostList(projectId, String.valueOf(entity.getId()));
    }

    @Override
    public void onEditClick() {

    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == REQUEST_TYPE_ADD && resultCode == REQUEST_TYPE_ADD_SUCCESS) {
            mEventPostPresenter.getPostList(projectId);
        }
    }


    @Override
    protected void onResume() {
        super.onResume();
        if (needRefresh) {
            mEventPostPresenter.getPostList(projectId);
            needRefresh = false;
        }
    }

    @Override
    protected void onDestroy() {
        EventBus.getDefault().unregister(this);
        super.onDestroy();
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void DataChange(DataChangeEvent event) {
        needRefresh = true;
    }


}
